/**
 * 
 */
package com.cuc.platform.core.componet.security.service.logic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;

import com.cuc.platform.core.componet.security.dao.SecurityRoleDao;
import com.cuc.platform.core.componet.security.domain.SecurityRole;
import com.cuc.platform.core.componet.security.service.SecurityRoleService;
import com.cuc.platform.core.componet.security.utils.SecurityIdentityTools;
import com.cuc.platform.core.service.ServiceException;

/**
 * @author peepul
 * 
 * 系统角色业务实现类
 */
public class SecurityRoleServiceImpl implements SecurityRoleService {

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityRoleService#getSecurityRole(java.lang.String)
	 */
	@Override
	public SecurityRole getSecurityRole(Long id) {
		if (null == id) return null;
		
		return securityRoleDao.getRoleById(id);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityRoleService#getAllRoles()
	 */
	@Override
	public List<SecurityRole> getAllRoles() {
		return securityRoleDao.getAllRoles();
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityRoleService#getUserWithRoles(java.lang.String)
	 */
	@Override
	public SecurityRole getRoleWithAuths(Long id) {
		if (null == id) return null;
		
		return securityRoleDao.getRoleWithAuths(id);
	}
	
	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityRoleService#insertRole(com.cuc.platform.core.componet.security.domain.SecurityRole)
	 */
	@Override
	public void insertRole(SecurityRole role) throws ServiceException {
		if (null == role) return;
		
		/* 赋默认值 */
		//非系统管理级用户
		String isAdmin = role.getIsAdmin();
		if (null == isAdmin || isAdmin.equals("")){
			role.setIsAdmin(SecurityIdentityTools.NO_ADMIN);
		}
		
		securityRoleDao.insertRole(role);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityRoleService#updateRole(com.cuc.platform.core.componet.security.domain.SecurityRole)
	 */
	@Override
	public void updateRole(SecurityRole role) throws ServiceException {
		if ((null == role) || (null == role.getId()) ) return;
		
		/* 赋默认值 */
		//非系统管理级用户
		String isAdmin = role.getIsAdmin();
		if (null == isAdmin || isAdmin.equals("")){
			role.setIsAdmin(SecurityIdentityTools.NO_ADMIN);
		}
		
		securityRoleDao.updateRole(role);
	}

	@Override
	public void delRole(Long id) throws ServiceException {
		if (null == id) return;
		
		try{
			int rv = securityRoleDao.delRoleById(id);
			
			//如果返回值为0,则没找到对应id的数据，抛出业务异常给web层
			if(rv == 0){
				logger.warn("没找到id为：" + id + "的数据，无效的删除操作。");
				throw new ServiceException("系统中不存在要删除的数据！");
			}
		}catch(DataAccessException ex){
			logger.error("该角色有关联的权限或用户，无效的删除操作。");
			throw new ServiceException("请先删除与用户或权限的关联，才能删除本条数据。");
		}
		
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityRoleService#findAuthswithRoles(java.lang.String)
	 */
	@Override
	public List<SecurityRole> findAuthswithRoles(Long roleId) {
		if (null == roleId) return null;
		
		return securityRoleDao.viewAuthswithRoles(roleId);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.service.SecurityRoleService#saveRoleAuthUnions(java.lang.String, java.lang.String[])
	 */
	@Override
	public void saveRoleAuthUnions(String role_id, String[] auth_ids) throws ServiceException {
		if (null == role_id) return;
		
		//首先删除该role_id对应的关联关系，以避免插入重复数据，然后再循环插入。
		securityRoleDao.delRoleAuthUnions(Long.valueOf(role_id));
		
		if (null == auth_ids ) return;

		/*插入多条关联数据。实际批量数据不多，所以没有采用Mybatis的批量插入方式*/
		//构造参数map
		Map<String, Long> map = new HashMap<String, Long>();
		map.put("role_id", Long.valueOf(role_id));
		for(String auth_id : auth_ids){
			map.put("auth_id", Long.valueOf(auth_id));
			securityRoleDao.saveRoleAuthUnions(map);
		}
	}
	
	/**
	 * private contents 
	 */
	//日志
	private static Logger logger = Logger.getLogger(SecurityResourceServiceImpl.class);
	//Ioc bean
	private SecurityRoleDao securityRoleDao;

	/****************************************************
	 * Setter methods for dependency injection								
	 ****************************************************/
	
	/**
	 * @param securityRoleDao the securityRoleDao to set
	 */
	public void setSecurityRoleDao(SecurityRoleDao securityRoleDao) {
		this.securityRoleDao = securityRoleDao;
	}
}
